package com.gcd.tempe;

import com.gcd.tempe.model.Barang;
import com.gcd.tempe.util.GlobalVariabel;
import com.gcd.tempe.util.JSON_Response;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LihatItemBarangActivity extends Activity {
	private int id;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item_barang);
		
		Bundle bundle = getIntent().getExtras();
		if (bundle.getString("nama") != null) {
			id = bundle.getInt("id");
			Log.d("pret", "id extra barang2 : " + bundle.getString("id"));
			Log.d("pret", "path gambar : " + bundle.getString("path_gambar"));
		
			Drawable image = JSON_Response.ImageOperations(this, bundle.getString("path_gambar"),"image.jpg");
			ImageView iv = (ImageView) findViewById(R.id.gambarbarang);
			iv.setAdjustViewBounds(true);
			iv.setMaxHeight(100);
			iv.setMaxWidth(100);
			iv.setImageDrawable(image);
			
			TextView tv = (TextView) findViewById(R.id.namaproduk);
			tv.setText(bundle.getString("nama"));
			tv.setGravity(Gravity.CENTER);
			
			TextView tv2 = (TextView) findViewById(R.id.deskripsiproduk);
			tv2.setText("("+bundle.getString("deskripsi")+")");
			tv.setGravity(Gravity.CENTER);
			
			TextView tv3 = (TextView) findViewById(R.id.hargaproduk);
			tv3.setText("Harga/Item : "+bundle.getString("harga"));
			
			TextView tv4 = (TextView) findViewById(R.id.jumlahproduk);
			tv4.setText("Jumlah Item Tersedia : " + bundle.getString("jumlah"));
		}
		
		//Cek udah login belum:
		if (LoginActivity.username==null) {
			//Menu add to cart diilangin
			View b = findViewById(R.id.orderID);
        	b.setVisibility(View.GONE);
		}
	}
	
	public void addToCart(View v) {
		//buat pesan barang
		
		//Isi ke variabel Global CartItems
		EditText et = (EditText) findViewById(R.id.editTextJumlahPesan);
		int numberOrder;
		String notFill = "";
		if (et.getText().toString().equals(notFill)) {
			Toast.makeText(LihatItemBarangActivity.this, "Maaf, Anda belum mengisi jumlah pesan", Toast.LENGTH_LONG).show();
		} else {
			numberOrder = Integer.parseInt(et.getText().toString());
			Bundle bundle = getIntent().getExtras();
			int jumlahproduk = Integer.parseInt(bundle.getString("jumlah"));
			
			if (jumlahproduk==0) {
				Toast.makeText(LihatItemBarangActivity.this, "Maaf, Produk sedang kosong", Toast.LENGTH_LONG).show();
			}
			else if (jumlahproduk<numberOrder) {
				Toast.makeText(LihatItemBarangActivity.this, "Maaf, Jumlah Produk kurang dari Jumlah Pesanan Anda", Toast.LENGTH_LONG).show();
			} else {
				if (bundle.getString("nama") != null) {
					Barang itemAdded = new Barang(bundle.getInt("id"), bundle.getInt("kategori_id"), bundle.getString("nama"), bundle.getString("path_gambar"), bundle.getString("deskripsi"), Integer.parseInt(bundle.getString("harga")) , numberOrder);
					GlobalVariabel.cartItems.addItemToItemsCart(itemAdded);
					Toast.makeText(LihatItemBarangActivity.this, "Barang berhasil ditambahkan ke Cart", Toast.LENGTH_LONG).show();
				}
			}
			
			//balik lagi ke acitivity lihat barang
			Intent intent = new Intent(LihatItemBarangActivity.this, LihatBarangActivity.class);
			startActivity(intent);
			
//			coba pake:
//			finish();
		}
	}
	

	
}
